package com.xiaodu.springboot.entity;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.http.HttpStatus;

/**
 * @author: dutianjie
 * @date: 2021/3/30
 * @version: v1.0.0
 * @description: 描述
 **/
@ApiModel(description = "http请求返回resultBean")
public class ResultBean<T> {

    @ApiModelProperty(name = "code", value = "代码值", required = true)
    private int code;

    @ApiModelProperty(name = "msg", value = "请求返回描述", required = false, dataType = "java.lang.String")
    private String msg;

    @ApiModelProperty(name = "data", value = "请求返回的具体内容")
    private T data;

    public ResultBean() {
    }

    public ResultBean(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResultBean(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> ResultBean<T> success(int code, String msg, T data) {
        return new ResultBean(code, msg, data);
    }

    public static <T> ResultBean<T> success(T data) {
        return result(HttpStatus.OK, data);
    }

    public static <T> ResultBean<T> error(int code, String msg) {
        return new ResultBean(code, msg);
    }

    public static <T> ResultBean error() {
        return result(HttpStatus.INTERNAL_SERVER_ERROR);
    }

    public static <T> ResultBean<T> result(HttpStatus status) {
        return new ResultBean(status.value(), status.getReasonPhrase());
    }

    public static <T> ResultBean<T> result(HttpStatus status, T data) {
        return success(status.value(), status.getReasonPhrase(), data);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "ResultBean{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
